function [QValuesSave, AbsorptionSave,PhiSave] = Spara2QandR(runIDs, ParameterRange, FrequencySelectRange, PolarizedDirection, FileTag, SaveFlag, TwoPeak)
    % 从当前打开的CST窗口，获取S参数，计算Q值和吸收值，保存在CST所在文件位置
    %
    % @since 2022.3.3
    % @param  [runIDs] runID向量.
    % @param  [ParameterRange] 对应的参数值变化范围.
    % @param  [FrequencySelectRange] 需要计算Q值的范围.
    % @param  [PolarizedDirection] 极化方向.
    % @param  [FileTag] 保存文件名中加入的标志.
    % @param  [SaveFlag] 是否保存文件，保存则不为0.
    % @param  [TwoPeak] 所选频率范围有两个峰是，计算第一个(-1)还是第二个(其他值).
    % @return  [QValuesSave] Q值，ID，和参数值.
    % @return  [AbsorptionSave] 频率与各个参数对应的吸收值.
    %
    % 使用案例1：
    %    无参数调用， [~,~] = Spara2QandA()
    %    将按runID返回所有值，Q值默认计算第一个峰，默认频率范围请查看
    %    极化方向为Y，不保存文件
    % 使用案例2：
    %    [~,~]=Spara2QandA(1:31,-85:5:60,[1.1,1.3],'Y','Full_Dx',0,-1)
    %

%     close all;
    CST_Post = CST_MicrowaveStudio();

    if nargin == 0
        runIDs = CST_Post.getRunIDs;
        FrequencySelectRange = [1.18, 1.28];
        PolarizedDirection = 'Y';
        FileTag = [];
        SaveFlag = 0;
        TwoPeak = -1;

    end

    % [~, runIDStrings] = CST_Post.getRunIDs();
    % runIDStrings = erase(runIDStrings, '3D:RunID:');
    % runIDStrings = str2num(char(runIDStrings));
    % ParameterRange = runIDStrings(runIDs);

    QValuesSave = [];
    AbsorptionSave = [];
    PhiSave = [];

    for everyrun = runIDs
        % fprintf('\n------本次处理的是runID:%i------------ \n', everyrun);

        [freq, sparam, ~] = CST_Post.getSParams(everyrun);

        if PolarizedDirection == 'Y'
            [Output_Q, Output_Abp,x,y,Phi] = Obtain_A_and_Q(sparam, freq, FrequencySelectRange);

        elseif PolarizedDirection == 'X'
            [Output_Q, Output_Abp] = Obtain_A_and_Q(sparam(:, 5:6), freq, FrequencySelectRange);
        end

        if TwoPeak == -1
            Output_Q = Output_Q(1);
            QValuesSave = [QValuesSave; Output_Q, everyrun,x,y];
            
        else
            Output_Q = Output_Q(end);
            QValuesSave = [QValuesSave; Output_Q, everyrun];
        end

        AbsorptionSave = [AbsorptionSave, Output_Abp];
        PhiSave = [PhiSave, Phi];
        fprintf(' runID:%d ----------- Q_Value:%f \n', everyrun, Output_Q);
        %         fprintf('\n-------------------------\n');
    end

    %-----------------
    figure;
    plot(1:length(runIDs), QValuesSave(:, 1), 'r*-', 'linewidth', 2);
    %     xlim([runIDs(1) runIDs(end)]);
    set(gca, 'xtick', 1:length(runIDs));
    set(gca, 'XTickLabel', ParameterRange);
    figure;
    contourf(AbsorptionSave);

    %-----------------
    if SaveFlag

        SaveFile([freq,AbsorptionSave], [CST_Post.folder, '\Fre_Absorption_', FileTag, ], 'txt');
%         SaveFile( [CST_Post.folder, '\Fre_Absorption_', FileTag, ], 'txt');
        SaveFile([QValuesSave,ParameterRange'], [CST_Post.folder, '\Q_', FileTag], 'txt');
        disp(['文件保存成功！'])
    end

    function [Output_Q, Output_Abp,x,y,Phi] = Obtain_A_and_Q(sparam, freq, FrequencySelectRange)
%         Absorption = 1 - abs(sparam(:, 1)).^2 - abs(sparam(:, 2)).^2;
 Absorption = abs(sparam(:, 1)).^2 + abs(sparam(:, 2)).^2; % 其实是反射
 Phi = angle(sparam(:,1));

        IndicsRange = find(FrequencySelectRange(1) <= freq & FrequencySelectRange(2) >= freq);
        SeperatedPeak = Absorption(IndicsRange);
        SeperatedFreq = freq(IndicsRange);
        clearvars sparm freq IndicsRange

        try
            [y, x, width, ~] = findpeaks(1-SeperatedPeak, SeperatedFreq, 'widthreference', 'halfheight');
            Q_Value = x ./ width;
            Q_Value(1); % 看一下是否为空
        catch ME

            if (strcmp(ME.identifier, 'MATLAB:badsubscript'))
                fprintf('*\n*\n*\n索引超出范围，具体请查看峰的形状（已经将Q值取为INf)*\n*\n*\n')
                Q_Value = Inf;
            end

        end

        Output_Abp = Absorption;
        Output_Q = Q_Value;

    end

end
